package com.app.xinfadi.widget.image_browser.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.xinfadi.R;
import com.app.xinfadi.widget.image_browser.ImageFloder;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.List;

/**
 * Created by HiWin10 on 2016/1/22.
 */
public class ImageBrowserAdapter extends BaseAdapter {

    private Context context;
    private List<ImageFloder> datas;
    private DisplayImageOptions options;
    private ImageSize mImageSize;

    public ImageBrowserAdapter(Context context, List<ImageFloder> datas) {
        this.context = context;
        this.datas = datas;
        mImageSize = new ImageSize(250, 250);
        options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.image_failed)
                .cacheOnDisk(true).bitmapConfig(Bitmap.Config.ARGB_4444).build();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int arg0) {
        return datas.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.image_browser_pop_list_item, null);
            holder = new ViewHolder();
            holder.id_dir_item_count = (TextView) convertView.findViewById(R.id.id_dir_item_count);
            holder.id_dir_item_image = (ImageView) convertView.findViewById(R.id.id_dir_item_image);
            holder.id_dir_item_name = (TextView) convertView.findViewById(R.id.id_dir_item_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.id_dir_item_name.setText(datas.get(position).getName());
        final ImageView iamgeview = holder.id_dir_item_image;
        ImageLoader.getInstance().loadImage("file://" + datas.get(position).getFirstImagePath(), mImageSize,
                options, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        super.onLoadingComplete(imageUri, view, loadedImage);
                        iamgeview.setImageBitmap(loadedImage);
                    }
                });
        holder.id_dir_item_count.setText(datas.get(position).getCount() + "张");
        return convertView;
    }

    class ViewHolder {
        ImageView id_dir_item_image;
        TextView id_dir_item_name;
        TextView id_dir_item_count;
    }

}